package com.cyandpos.pad.querygoods.model;

import android.text.TextUtils;

import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.Goods;
import com.google.gson.JsonObject;



public class QueryGoodsModelImpl implements QueryGoodsModel {
    @Override
    public void loadQueryGoods(String goodsCode,String personDeptId, final OnLoadQueryGoodsListener listener) {
        //发起网络请求 成功回调
        PosHttpManager.QueryGood(goodsCode, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<Goods> baseResponse = BaseResponse.fromJson(response.toString(),Goods.class);
                if(baseResponse.getCode().equals("0")){
                    try {
                        listener.onLoadQueryGoodsSuccess(baseResponse.getData());
                    }catch (Exception e){
                        listener.onLoadQueryGoodsFailure(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }
                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onLoadQueryGoodsFailure(baseResponse.getMsg());
                    }else {
                        listener.onLoadQueryGoodsFailure(baseResponse.getTips());

                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onLoadQueryGoodsFailure(msg);

            }
        });

    }

    public interface OnLoadQueryGoodsListener{
        void onLoadQueryGoodsSuccess(Goods goods);
        void onLoadQueryGoodsFailure(String errorMsg);

    }

}
